<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2230 – `which` is non-standard. Use builtin `command -v` instead.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2230 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2230">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2
id="which-is-non-standard-use-builtin-command--v-instead"><code>which</code>
is non-standard. Use builtin <code>command -v</code> instead.</h2>
<p>This is an <a href="optional.html">optional</a> suggestion. It must be
explicitly enabled with a <a href="directive.html">directive</a>
<code>enable=deprecate-which</code> in a <code># shellcheck</code>
comment or <code>.shellcheckrc</code></p>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2230.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">which</span> grep</span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2230.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="co"># For the path of a single, unaliased, external command,</span></span>
<span id="cb2-2"><a href="SC2230.html#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="co"># or to check whether this will just &quot;run&quot; in this shell:</span></span>
<span id="cb2-3"><a href="SC2230.html#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="bu">command</span> <span class="at">-v</span> grep</span>
<span id="cb2-4"><a href="SC2230.html#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="co"># To check whether commands exist, without obtaining a reusable path:</span></span>
<span id="cb2-5"><a href="SC2230.html#cb2-5" aria-hidden="true" tabindex="-1"></a><span class="bu">hash</span> grep</span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p><code>which</code> is a non-standard, external tool that locates an
executable in PATH. <code>command -v</code> is a POSIX standard builtin,
which uses the same lookup mechanism that the shell itself would.</p>
<h3 id="exceptions">Exceptions:</h3>
<p>This check is opt-in only in 0.7.1+, and you may choose to <a
href="ignore.html">ignore</a> it in earlier versions. <code>which</code> is
very common, and some prefer its executable-or-nothing behavior over
<code>command -v</code>'s handling of builtins, functions and
aliases.</p>
<h3 id="caveats">Caveats:</h3>
<h4
id="command--v-does-not-check-all-parameters"><code>command -v</code>
does not check ALL parameters</h4>
<p><code>command -v</code> succeeds (with exit code 0) if <em>any</em>
command exists:</p>
<pre><code># grep is in /usr/bin/grep
# foobar is not in path
#
$ command -v -- grep foobar; echo $?
0</code></pre>
<p>In the above example, it should have failed and exited with 1 unless
<em>all</em> commands exist, if it were to be a replacement for
<code>which</code>. Other problems associated with <code>command</code>
include its inclusion of builtins, aliases, and functions.</p>
<p>An alternative is:</p>
<pre><code>$ hash &lt;file1&gt; &lt;file2&gt;</code></pre>
<p>Which observes the standard behaviour of failures.</p>
<p>To obtain a path, <code>type -p</code> can be used instead. Like
<code>command -v</code>, it has a similarly quirky behavior with
builtins, aliases, and functions, although this is arguably milder since
it would print nothing for these cases. The failure condition is similar
to <code>hash</code>.</p>
<h3 id="related-resources">Related resources:</h3>
<ul>
<li><code>shellcheck</code> issue: <a
href="https://github.com/koalaman/shellcheck/issues/1162">#1162 command
-v is not a direct replacement for which</a> (Discussion)</li>
<li><a href="https://stackoverflow.com/a/677212/1899640">Check if a
program exists from a Bash script</a> on StackOverflow.</li>
</ul>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


